package com.widget.dialog;

import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.mylove.judas.yaya.R;
import com.mylove.judas.yaya.common.utils.DensityUtil;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

public class ShowImagesDialog extends Dialog {

    private Context context;
    private View view ;
    private PhotoView photoView;
    String url;

    public ShowImagesDialog(@NonNull Context context, String url) {
        super(context, R.style.transparentBgDialog);  //这里用到了自定义Dialog的样式
        this.context = context;
        this.url = url;

        initView();
        initData();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(view);
        Window window = getWindow();
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.x = 0;
        wl.y = 0;
//        wl.height = Config.EXACT_SCREEN_HEIGHT;
//        wl.width = Config.EXACT_SCREEN_WIDTH;
        wl.height = DensityUtil.screenHeight;
        wl.width = DensityUtil.screenWidth;
        wl.gravity = Gravity.CENTER;
        window.setAttributes(wl);
    }

    private void initView() {
        view = View.inflate(context, R.layout.dialog_image, null);
        photoView = (PhotoView) view.findViewById(R.id.pv_long);
    }

    private void initData(){
        PhotoViewAttacher.OnPhotoTapListener listener = new PhotoViewAttacher.OnPhotoTapListener() {
            @Override
            public void onPhotoTap(View view, float x, float y) {
                dismiss();
            }
        };

        photoView.setOnPhotoTapListener(listener);

        Glide.with(context)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher)
                .into(new SimpleTarget<GlideDrawable>() { //这个灰常重要，作用是让图片清晰地显示
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        photoView.setImageDrawable(resource);
                    }
                });
    }
}